package com.wandong.cinema.entity;

import com.wandong.cinema.service.UserService;
import com.wandong.cinema.service.impl.UserServiceImpl;
import com.wandong.cinema.util.InputUtil;

import java.util.Date;

public class Admin extends User {
    private UserService userService = new UserServiceImpl();

    public Admin(String userId, String username, String password, Date registerTime, String userType, String phone) {
        super(userId, username, password, registerTime, userType, phone);
    }

    @Override
    public void showMenu() {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 管理用户");
            System.out.println("2. 查看系统日志");
            System.out.println("3. 修改个人信息");
            System.out.println("4. 退出登录");
            System.out.print("请选择操作: ");
            
            int choice = InputUtil.inputInteger();
            switch (choice) {
                case 1:
                    manageUsers();
                    break;
                case 2:
                    viewLogs();
                    break;
                case 3:
                    updatePersonalInfo();
                    break;
                case 4:
                    System.out.println("退出登录成功");
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    private void manageUsers() {
        while (true) {
            System.out.println("\n=== 用户管理 ===");
            System.out.println("1. 添加用户");
            System.out.println("2. 删除用户");
            System.out.println("3. 修改用户");
            System.out.println("4. 查询用户");
            System.out.println("5. 返回上一级");
            System.out.print("请选择操作: ");
            
            int choice = InputUtil.inputInteger();
            switch (choice) {
                case 1:
                    userService.addUser();
                    break;
                case 2:
                    userService.deleteUser();
                    break;
                case 3:
                    userService.updateUser();
                    break;
                case 4:
                    userService.queryUsers();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    private void viewLogs() {
        System.out.println("\n=== 系统日志 ===");
        try {
            java.nio.file.Path path = java.nio.file.Paths.get("cinema_log.txt");
            java.util.List<String> lines = java.nio.file.Files.readAllLines(path);
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println("读取日志文件失败: " + e.getMessage());
        }
    }


    private void updatePersonalInfo() {
        System.out.println("\n=== 修改个人信息 ===");
        System.out.print("请输入新密码: ");
        String newPassword = InputUtil.inputString();
        System.out.print("请输入新手机号: ");
        String newPhone = InputUtil.inputString();
        
        setPassword(newPassword);
        setPhone(newPhone);
        System.out.println("个人信息修改成功");
    }
}
